package com.dwxt.sport.business.service;

import com.alibaba.fastjson.JSON;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.StreetMapper;
import com.dwxt.sport.business.entity.Street;
import com.dwxt.sport.business.vo.street.SelectStreetVo;
import com.dwxt.sport.business.vo.street.InsertStreetVo;
import com.dwxt.sport.business.vo.street.UpdateStreetVo;
import com.dwxt.sport.exception.CustomException;
import com.dwxt.sport.util.BusinessValidationUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 街道 Service
 *
 * @author Kuang.JiaZhuo
 * @date 2020-07-24 16:33
 */
@Service
public class StreetService {

    @Resource
    StreetMapper streetMapper;

    /**
     * @方法名：insertStreet
     * @描述： 新增街道
     * @作者： kjz
     * @日期： Created in 2020/7/24 16:54
     */
//    @Transactional(rollbackFor = Exception.class)
//    public int insertStreet(Street street) {
//        street.setId(SnowIdUtil.getInstance().nextId())
//                .setCreateTime(LocalDateTime.now())
//                .setIsDelete(VariableConstants.STRING_CONSTANT_0);
//        return streetMapper.insert(street);
//    }

    /**
     * @方法名：createStreet
     * @描述： 新增街道
     * @作者： kjz
     * @日期： Created in 2020/7/27 12:19
     */
    @Transactional(rollbackFor = Exception.class)
    public int createStreet(InsertStreetVo insertStreetVo) {
        Street street = new Street()
                .setId(SnowIdUtil.getInstance().nextId())
                .setCreateTime(LocalDateTime.now())
                .setIsDelete(VariableConstants.STRING_CONSTANT_0)
                .setName(insertStreetVo.getName());
        return streetMapper.insert(street);
    }

    /**
     * @方法名：updateStreet
     * @描述： 更新街道
     * @作者： kjz
     * @日期： Created in 2020/7/24 17:55
     */
//    @Transactional(rollbackFor = Exception.class)
//    public int updateStreet(Street street) {
//        //更新需传入id
//        if(null == street.getId() || "".equals(street.getId())){
//            throw new CustomException(StatusCode.ID_IS_NULL);
//        }
//
//        street.setUpdateTime(LocalDateTime.now());
//        return streetMapper.updateByPrimaryKeySelective(street);
//    }

    /**
     * @方法名：modifyStreet
     * @描述： 更新街道
     * @作者： kjz
     * @日期： Created in 2020/7/27 12:35
     */
    @Transactional(rollbackFor = Exception.class)
    public int modifyStreet(UpdateStreetVo updateStreetVo) {
        String id = updateStreetVo.getId();
        if(!BusinessValidationUtil.verifyId(id)){
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        Street street = new Street()
                .setId(id)
                .setUpdateTime(LocalDateTime.now())
                .setName(updateStreetVo.getName());
        return streetMapper.updateByPrimaryKeySelective(street);
    }

    /**
     * @方法名：getStreetListByPage
     * @描述： 按查询条件获取街道列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/7/24 18:20
     */
    public PageInfo<Street> getStreetListByPage(SelectStreetVo selectStreetVo) {
        PageHelper.startPage(selectStreetVo.getPageNum(), selectStreetVo.getPageSize());
        System.err.println(JSON.toJSON(selectStreetVo));
        List<Street> list = streetMapper.getStreetListByCondition(selectStreetVo);
        PageInfo<Street> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteStreetByIds
     * @描述： 传入ids批量删除街道
     * @作者： kjz
     * @日期： Created in 2020/7/24 18:41
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteStreetByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }

        String[] idArray = ids.split(",");
        //删除消息和工作人员的关联
        return this.streetMapper.deleteStreetByIds(idArray);
    }


}
